package Operation;//操作包

import mybook.MyBook;
import mybook.MyBookList;


import java.util.Scanner;

//删除图书类
public class Delete implements IOperation{

    @Override
    public void work(MyBookList myBookList) {
        System.out.println("删除图书");

        int currentCount = myBookList.getCount();

        if (currentCount == 0) {

            System.out.println("书架为空，无法删除");

            return;
        }

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入书名:");

        String name = scanner.nextLine();

        int index = -1;

        for (int i = 0; i < currentCount; i++) {
            MyBook mybook = myBookList.getBook(i);
            if (mybook.getTitle().equals(name)) {
                index = i;
                break;
            }

        }
        if (index == -1) {
            System.out.println("暂时不存在这本书");
            return;
        }
        for (int i = index; i < currentCount - 1; i++) {
            MyBook mybook = myBookList.getBook(i + 1);
            myBookList.setBook(i, mybook);
        }
        System.out.println("图书删除成功");
        myBookList.setCount(currentCount - 1);

    }
}
